package com.aopcinemas.cinema.behaviour;

import jade.content.lang.Codec.CodecException;
import jade.content.onto.OntologyException;
import jade.content.onto.basic.Action;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.AchieveREResponder;

import com.aopcinemas.cinema.CinemaAgent;
import com.aopcinemas.cinema.dao.ScheduleDao;
import com.aopcinemas.cinema.ontology.GetScheduleList;

public class ScheduleInfoBehaviour extends AchieveREResponder {

    private static final long serialVersionUID = 1L;

    private final ScheduleDao scheduleDao;

    public ScheduleInfoBehaviour(ScheduleDao scheduleDao, CinemaAgent agent, MessageTemplate tmpl) {
        super(agent, tmpl);
        this.scheduleDao = scheduleDao;
    }

    @Override
    protected ACLMessage handleRequest(ACLMessage request) throws NotUnderstoodException, RefuseException {
        try {
            Action action = (Action) myAgent.getContentManager().extractContent(request);
            action.setActor(request.getSender());
            GetScheduleList getScheduleList = (GetScheduleList) action.getAction();
            getScheduleList.setSchedules(scheduleDao.getShedules(getScheduleList));
            ACLMessage reply = request.createReply();
            reply.setPerformative(ACLMessage.INFORM);
            System.out.println("Ontology: " + reply.getOntology() + ", " + reply.getLanguage());

            myAgent.getContentManager().fillContent(reply, action);
            return reply;
        } catch (CodecException e) {
            e.printStackTrace();
        } catch (OntologyException e) {
            e.printStackTrace();
        }
        throw new RuntimeException("Something is wrong");
    }
}
